/*
 * ComentariServlet.java
 *
 */

package org.wooxes.server;

import org.wooxes.client.Comment;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.net.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author woox
 */
public class ComentariServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //String directori_s = request.getParameter("directori");
        String accio_s = request.getParameter("accio");
        String arxiu_s = request.getParameter("arxiu");
        String linia_s = request.getParameter("linia");
        int linia = Integer.parseInt(linia_s);
        String comentari = request.getParameter("comentari");
        String tipus = request.getParameter("tipus");
        
        // Els arxius de comentari tindran el mateix nom que l'original
        // afegint l'extensió .comment
        File arxiu = new File(arxiu_s + ".comment");
        
        ArxiuXML xml = new ArxiuXML(arxiu);
        
        if (accio_s.equals("afegir")){
            // Si l'arxiu de comentaris no existeix, es crea
            Comment comment = new Comment(linia, comentari, tipus);

            if(!xml.addComment(comment)){
                out.println("Error: No s'ha pogut afegir el comentari");
                return;
            }
        }
        // Sinó, accio_s.equals("eliminar")
        else {
            if(!arxiu.exists()){
                out.println("Error: No es troba l'arxiu");
                return;
            }
            if(!xml.eliminarComentari(linia)){
                out.println("Error: No s'ha pogut eliminar el comentari");
                return;
            }
        }
        out.println("ok");

        //System.out.println("Arxiu: " + arxiu.toString());
        //System.out.println("Comentari: " + comentari);
        out.close();
    } 
        
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
